// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.core.shadow;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.dengues.commons.connectivity.IConnectivitySettingsConstants;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.components.EGlobalComponentNames;
import org.dengues.core.components.IXMLComponent;
import org.dengues.core.external.AbstractCompNodeExt;
import org.dengues.core.metadata.MetadataColumn;
import org.dengues.core.metadata.MetadataTable;
import org.dengues.core.process.CompElement;
import org.dengues.core.process.ICompGefNode;
import org.dengues.core.process.ICompNode;
import org.dengues.core.process.ICompProcess;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ShadowCompNode extends CompElement implements ICompNode, ICompGefNode {

    private static final long serialVersionUID = 145648874322L;

    private final IXMLComponent component;

    private ShadowCompProcess compProcess;

    private int columnNumber = 255;

    /**
     * Qiang.Zhang.Adolf@gmail.com ShadowCompNode constructor comment.
     * 
     * @param compProcess
     * @param component
     */
    public ShadowCompNode(ShadowCompProcess compProcess, IXMLComponent component) {
        this.component = component;
        this.compProcess = compProcess;
        init(component);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com ShadowCompNode constructor comment.
     */
    public ShadowCompNode(EGlobalComponentNames componentName) {
        this(DenguesCorePlugin.getDefault().getDesignerCoreService().getComponentsFactory().getComponent(componentName.getName()));
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com ShadowCompNode constructor comment.
     * 
     * @param component
     */
    public ShadowCompNode(IXMLComponent component) {
        this.component = component;
        init(component);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com ShadowCompNode constructor comment.
     * 
     * @param component
     * @param properties
     */
    public ShadowCompNode(IXMLComponent component, Properties properties) {
        this(component);
        Enumeration baseProperties = properties.keys();
        while (baseProperties.hasMoreElements()) {
            String id = (String) baseProperties.nextElement();
            if (id.startsWith(IConnectivitySettingsConstants.PROP_PREFIX)) {
                String string = id.substring(IConnectivitySettingsConstants.PROP_PREFIX.length());
                setNodeParameterValue(string.toUpperCase(), properties.get(id));
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "init".
     * 
     * @param component2
     */
    private void init(IXMLComponent component2) {
        setElementParameters(component.createNodeParameters(this));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompNode#getCompName()
     */
    @Override
    public String getCompName() {
        return component.getName();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompNode#getComponent()
     */
    public IXMLComponent getComponent() {
        return component;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompNode#getUniqueName()
     */
    public String getUniqueName() {
        return "shadow_" + component.getName(); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompNode#isStart()
     */
    public boolean isStart() {
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompNode#isSubProcessStart()
     */
    public boolean isSubProcessStart() {
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.CompElement#getCompProcess()
     */
    @Override
    public ICompProcess getCompProcess() {
        return compProcess;
    }

    /**
     * Sets the compProcess.
     * 
     * @param compProcess the compProcess to set
     */
    public void setCompProcess(ShadowCompProcess compProcess) {
        this.compProcess = compProcess;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompNode#getExternalNode()
     */
    public AbstractCompNodeExt getExternalNode(boolean isPrompt) {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompNode#getSubProcessStartNode(boolean)
     */
    public ICompNode getSubProcessStartNode(boolean main) {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.CompElement#getMetadataList()
     */
    @Override
    public List<MetadataTable> getMetadataList() {
        List<MetadataTable> metaList = super.getMetadataList();
        if (metaList == null || metaList.isEmpty()) {
            metaList = new ArrayList<MetadataTable>();
            MetadataTable table = new MetadataTable();
            table.setTableName(getUniqueName());
            for (int i = 0; i < columnNumber; i++) {
                MetadataColumn column = new MetadataColumn();
                column.setType("VARCHAR");
                column.setColumnName("row" + i);
                column.setDefault("");
                table.getColumns().add(column);
            }
            metaList.add(table);
        }
        return metaList;
    }

    /**
     * Sets the columnNumber.
     * 
     * @param columnNumber the columnNumber to set
     */
    public void setColumnNumber(int columnNumber) {
        this.columnNumber = columnNumber;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompGefNode#isExternalNode()
     */
    public boolean isExternalNode() {
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompGefNode#isModified()
     */
    public boolean isModified() {
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.core.process.ICompGefNode#setModified(boolean)
     */
    public void setModified(boolean modified) {
    }

}
